package edu.hit.pact.pgse.timer;

import java.util.Timer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.hit.pact.pgse.client.ClientManager;
import edu.hit.pact.pgse.client.handler.ClientMasterHandler;
import edu.hit.pact.pgse.util.Globals;
/*
 * need to schedule four kinds of tasks,
 * first one is to reload the conf.properity into Globals every hour
 * second is to schedule the web task on 0:00
 * third is to run the blog task on 0:00
 * forth is to run the bbs task from 0:00 every hour
 */

public class ClientTaskScheduler extends ConfTaskScheduler{

	private Timer webTaskTimer;
	private Timer blogTaskTimer;
	private Timer bbsTaskTimer;
	private Log logger;
	private ClientManager clientManager;
	
	public ClientTaskScheduler(ClientManager clientManager){
		super();
		webTaskTimer = new Timer();
		blogTaskTimer = new Timer();
		bbsTaskTimer = new Timer();
		logger = LogFactory.getLog(ClientTaskScheduler.class);
		this.clientManager = clientManager;
	}
	
	public void scheduleWebTask(){
		SuperTimerTask webTimerTask = new WebTimerTask(clientManager);
		long delayTime = TimeCalculator.nextDelayTime(0, 1, 0);
		webTaskTimer.scheduleAtFixedRate(webTimerTask, delayTime, Globals.INTERVAL_CRAWL_WEB_TASK*3600000);
		webTaskTimer.scheduleAtFixedRate(webTimerTask, 5000, Globals.INTERVAL_CRAWL_WEB_TASK*3600000);
		logger.info("scheduleWebTask");
	}
	
	public void scheduleBlogTask(){
		BlogTimerTask blogTimerTask = new BlogTimerTask(clientManager);
		long delayTime = TimeCalculator.nextDelayTime(Globals.TIME_BLOG_TASK_CRAWL_HOUR, Globals.TIME_BLOG_TASK_CRAWL_MINUTE, 0);
//		blogTaskTimer.scheduleAtFixedRate(blogTimerTask, delayTime, Globals.INTERVAL_CRAWL_BLOG_TASK*3600000);
		webTaskTimer.scheduleAtFixedRate(blogTimerTask, 5000, Globals.INTERVAL_CRAWL_WEB_TASK*3600000);
		logger.info("scheduleBlogTask");
	}
	
	public void scheduleBbsTask(){
		//BbsTimerTask bbsTimerTask = new BbsTimerTask(clientManager);
		//long delayTime = TimeCalculator.nextDelayTime(Globals.TIME_BBS_TASK_CRAWL_HOUR, Globals.TIME_BBS_TASK_CRAWL_MINUTE, 0);
		//long delayTime = TimeCalculator.nextDelayTime(0, 1, 0);
		//bbsTaskTimer.scheduleAtFixedRate(bbsTimerTask, delayTime, Globals.INTERVAL_CRAWL_BBS_TASK*3600000);
		//webTaskTimer.scheduleAtFixedRate(bbsTimerTask, 5000, Globals.INTERVAL_CRAWL_WEB_TASK*360000000);
		clientManager.pushBbsTask();
		logger.info("scheduleBbsTask");
	}
	
	public void reScheduleTask(String type){
		if (type.equals("web")){
			logger.error("reScheduleTask web");
			webTaskTimer.cancel();
			webTaskTimer = new Timer();
			
			scheduleWebTask();
		} else if (type.equals("blog")){
			logger.error("reScheduleTask blog");
			blogTaskTimer.cancel();
			blogTaskTimer = new Timer();
			
			scheduleBlogTask();
		} else if (type.equals("bbs")){
			//logger.error("reScheduleTask bbs");
			logger.error("never reschedule bbs task.");
			bbsTaskTimer.cancel();
			bbsTaskTimer = new Timer();
			//scheduleBbsTask();
		} else if (type.equals("conf")){
			logger.error("reScheduleTask conf");
			confTaskTimer.cancel();
			confTaskTimer = new Timer();
			super.scheduleConfTask();
		} else {
			logger.error("type wrong");
		}
	}
	
	public static void main(String[] args){
		/*
		 * load log4j property.
		 */
		Globals.readLog4jProperties();
		ClientTaskScheduler clientTaskScheduler = new ClientTaskScheduler(new ClientManager());
		clientTaskScheduler.scheduleConfTask();
		clientTaskScheduler.scheduleWebTask();
		clientTaskScheduler.scheduleBlogTask();
		clientTaskScheduler.scheduleBbsTask();
	}
}
